import ComponentsMeta from '../../AssetPack/web/web-components/components.json';

if (process.env.NODE_ENV === 'development') {
  window._adoPremise = {
    uc: {
      theme: 'light', // light | dark
      languages: {},
    },
  };
}

// 获取url传参
const getQueryValue = name => {
  const reg = new RegExp(`${name}=([^&]*)(&|$)`, 'i');
  const r = window.location.href.match(reg);
  if (r !== null) {
    return unescape(r[1]);
  }
  return null;
};

// 获取当前系统语言
export const getLang = () => {
  let lang = getQueryValue('lang');
  if (!lang) {
    lang = (window._adoPremise && window._adoPremise.language) || 'zh-CN';
  }
  return lang;
};

// 获取多语言
export const $demoi18n = (key, arr) => {
  // const langs = window._adoPremise.uc.languages.VisualComponents;
  // const langs = window._adoPremise.uc.languages.VisualComponentsSmartMining;
  const langs = window._adoPremise.uc.languages[ComponentsMeta.i18nTag];

  // 没有取到国际化即返回key值
  if (!langs || langs[key] === null || langs[key] === undefined) {
    return key;
  }

  // 没有传参即返回国际化值
  if (!arr || arr.length === 0) {
    return langs[key];
  }

  // 有传参，则替换传参后返回值
  let res = langs[key];
  arr.forEach((arrStr, index) => {
    const str = `\\{${index}\\}`;
    res = res.replace(new RegExp(`${str}`, 'g'), arrStr);
  });
  return res;
};
